package com.empire.module.member.controller.admin.cases;

import com.empire.module.member.controller.admin.cases.vo.CasesPageReqVO;
import com.empire.module.member.controller.admin.cases.vo.CasesRespVO;
import com.empire.module.member.controller.admin.cases.vo.CasesSaveReqVO;
import com.empire.module.member.dal.dataobject.cases.CasesDO;
import com.empire.module.member.service.cases.CasesService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.util.List;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 案件管理")
@RestController
@RequestMapping("/member/cases")
@Validated
public class CasesController {

    @Resource
    private CasesService casesService;

    @PostMapping("/create")
    @Operation(summary = "创建案件管理")
    @PreAuthorize("@ss.hasPermission('person:cases:create')")
    public CommonResult<String> createCases(@Valid @RequestBody CasesSaveReqVO createReqVO) {
        return success(casesService.createCases(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新案件管理")
    @PreAuthorize("@ss.hasPermission('person:cases:update')")
    public CommonResult<Boolean> updateCases(@Valid @RequestBody CasesSaveReqVO updateReqVO) {
        casesService.updateCases(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案件管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('person:cases:delete')")
    public CommonResult<Boolean> deleteCases(@RequestParam("id") String id) {
        casesService.deleteCases(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案件管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('person:cases:query')")
    public CommonResult<CasesRespVO> getCases(@RequestParam("id") String id) {
        CasesDO cases = casesService.getCases(id);
        return success(BeanUtils.toBean(cases, CasesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案件管理分页")
    @PreAuthorize("@ss.hasPermission('person:cases:query')")
    public CommonResult<PageResult<CasesRespVO>> getCasesPage(@Valid CasesPageReqVO pageReqVO) {
        PageResult<CasesDO> pageResult = casesService.getCasesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CasesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案件管理 Excel")
    @PreAuthorize("@ss.hasPermission('person:cases:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCasesExcel(@Valid CasesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CasesDO> list = casesService.getCasesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "案件管理.xls", "数据", CasesRespVO.class,
                        BeanUtils.toBean(list, CasesRespVO.class));
    }

}